package com.mcxx.modules.temporary.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.temporary.service.TempCompletedService;
import com.mcxx.modules.temporary.service.TempSalvationCompleteService;
import com.mcxx.modules.miniSalvation.order.dto.NoticeDTO;
import com.mcxx.modules.miniSalvation.order.service.read.OrderReadService;
import com.mcxx.modules.temporary.cancel.dto.TmpCancelSalvationDTO;
import com.mcxx.modules.temporary.order.param.OrderListSearchParam;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Validated
@Api("临时救助已办列表")
@RestService({"temporary/salvation/complete/v2"})
public class TempSalvationCompleteController extends DefaultRestService {

    private static final Logger logger = LoggerFactory.getLogger(TempSalvationCompleteController.class);

    @Autowired
    TempCompletedService completedService;
    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private TmpOrderReadService tmpOrderReadService;
    @Autowired
    private TempSalvationCompleteService tempSalvationCompleteService;


    @PostMapping("list_town")
    @ApiOperation("查询乡镇已办列表")
    public void listFinishedOrderTown(OrderListSearchParam param, PageParam pageParam){
        logger.info("查询乡镇已办列表，参数：{}，分页: {}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        param.setBusinessType(Constant.BusinessCode.LINSHI);
        Page page = tmpOrderReadService.listFinishedOrderTown(param, pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询乡镇已办列表完成，结果：{}",JSON.toJSONString(page));
    }


    @PostMapping("list_county")
    @ApiOperation("查询区县已办列表")
    public void listFinishedOrderCounty(OrderListSearchParam param, PageParam pageParam){
        logger.info("查询区县已办列表，参数：{}，分页: {}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        param.setBusinessType(Constant.BusinessCode.LINSHI);
        Page page = tmpOrderReadService.listFinishedOrderCounty(param, pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询区县已办列表完成，结果：{}",JSON.toJSONString(page));
    }



    @PostMapping("isCanPrintNotices")
    @ApiOperation("能否打印不受理通知书")
    public void isCanPrintNotices(@RequestParam("orderId")String orderId){
        logger.info("能否打印不受理通知书，参数：{}",orderId);
        int i = tmpOrderReadService.isCanPrintNotice(orderId);
        setData(i);
        logger.info("能否，结果：{}",JSON.toJSONString(i));
    }


    @PostMapping("printNotices")
    @ApiOperation("查询信息打印不受理通知书")
    public void printNotices(@RequestParam("orderId")String orderId){
        logger.info("打印不受理通知书，参数：{}",orderId);
        NoticeDTO noticesInfo = tmpOrderReadService.getNoticesInfo(orderId, getCurUserAccount());
        setData(noticesInfo);
        logger.info("打印不受理通知书，结果：{}",JSON.toJSONString(noticesInfo));
    }


    @PostMapping("downLoadListTown")
    @ApiOperation(value="导出临时救助乡镇已办信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出临时救助乡镇已办信息")
    public void downLoadListTown(HttpServletResponse response, @RequestBody OrderListSearchParam param)throws IOException {
        logger.info("开始导出临时救助乡镇已办信息，参数是：{}", param);
        param.setBusinessType(Constant.BusinessCode.LINSHI);
        ExcelInfo excelInfo = tempSalvationCompleteService.exportExcelSave(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("开始导出临时救助乡镇已办信息，查询结果是：{}", excelInfo);
    }



    @PostMapping("downLoadListCounty")
    @ApiOperation(value="导出临时救助区县已办信息", produces = MediaType.APPLICATION_JSON_UTF8_VALUE,notes="导出临时救助区县已办信息")
    public void downLoadListCounty(HttpServletResponse response, @RequestBody OrderListSearchParam param)throws IOException {
        logger.info("开始导出临时救助区县已办信息，参数是：{}", param);
        param.setBusinessType(Constant.BusinessCode.LINSHI);
        ExcelInfo excelInfo = tempSalvationCompleteService.exportExcelSave(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("开始导出临时救助区县已办信息，查询结果是：{}", excelInfo);
    }



    @PostMapping("canCancelSalvation")
    @ApiOperation("查询已办结业务能否撤销")
    public void canCancelSalvation(@V("notnull")String orderId) {
        logger.info("查询已办结业务能否撤销，参数：{}", orderId);
        int i = tempSalvationCompleteService.canCancelSalvation(orderId);
        setData(i);
        logger.info("查询已办结业务能否撤销完成，结果：{}",i);
    }


    @PostMapping("handleCancelSalvation")
    @ApiOperation("撤销已办结业务")
    public void handleCancelSalvation(TmpCancelSalvationDTO dto) {
        logger.info("撤销已办结业务，参数：{}", dto);
        tempSalvationCompleteService.handleCancelSalvation(dto,getCurUserAccount());
        logger.info("撤销已办结业务完成");
    }
}
